package com.version1_1.controller.ThirdPart.RongCloud;

import com.util.property.Properties;
import com.version1_1.service.RongCloudService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * Created by reeves on 2015/12/1.
 */

@Controller
public class RongCloudController {
    @Autowired
    private RongCloudService rongCloudService;

    private static final Logger LOGGER = Logger.getLogger(RongCloudController.class);


    /**
     * 使用第三方SDK，融云即时通讯模块，获取用户Token
     *
     * @Descripiton: 调用接口地址：http://121.40.188.62/kuibuversion1/thirdPart/user/getToken/{userId}
     */
    @RequestMapping(value = "/thirdPart/user/getToken/{userId}", method = RequestMethod.GET, produces = "text/json;charset=UTF-8")
    @ResponseBody
    public String getToken(@PathVariable("userId") int userId) {
        return rongCloudService.getToken(Properties.RongCloud_appKey, Properties.RongCloud_appSecret, userId);
    }

    /**
     * 根据userId获取用户信息
     *
     * @Descripiton: 接口调用地址：http://121.40.188.62/kuibuversion1/thirdPart/user/getUserInfo/{userId}
     */
    @RequestMapping(value = "/thirdPart/user/getUserInfo/{userId}", method = RequestMethod.GET, produces = "text/json;charset=UTF-8")
    @ResponseBody
    public String getUserInfo(@PathVariable("userId") int userId) throws UnsupportedEncodingException {
        /**
         * {"code":0} / {"code":1,"userName":"","userPhoto":""}
         */
        return rongCloudService.getUserInfo(userId);
    }


    /**
     * 添加好友，同意添加好友，拒绝添加好友
     *
     * 接口调用地址：http://121.40.188.62/kuibuversion1/thirdPart/user/addFriends
     */
    @RequestMapping(value = "/thirdPart/user/addFriends",method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
    @ResponseBody
    public String addFriends(HttpServletRequest request) throws UnsupportedEncodingException {
        /**
         * 添加好友功能，使用融云发送系统消息功能，对于融云的消息机制还有不理解的地方
         */

        /**
         * {"fromUserId":"","toUserId":"","content":""}
         */

        String addFriendsStr = request.getParameter("addFriendsStr");
        if (addFriendsStr == null) {
            LOGGER.error("添加好友出错,服务器未接收到请求Json");
            return "{\"addFriendsResult\":false}";
        }

        return rongCloudService.addFriends(addFriendsStr);
    }
}
